#include "sysmanage.h"
#include "adc_dev.h"
#include "time.h"
#include "comm.h"

ALIGN(RT_ALIGN_SIZE)
devInfodef devInfoDat = {0};

/**
  * @brief  EEPROM 驱动打开函数
  * @param  None
  * @retval 驱动对象
  */
rt_device_t eeprom_device_open(void)
{
	rt_device_t device;

	device = rt_device_find("EEPROM");
  if (device != RT_NULL)
  {
  	if(!(device->open_flag & RT_DEVICE_OFLAG_OPEN))
  	{
      rt_device_open(device,RT_DEVICE_OFLAG_RDWR);
  	}
  }
	else
	{
		RT_ASSERT(RT_NULL);
	}

  return device;
}

/**
  * @brief  EEPROM 驱动关闭
  * @param  EEPROM 驱动对象
  * @retval None
  */
void eeprom_device_close(rt_device_t dev)
{
	if(dev != RT_NULL)
	{
		rt_device_close(dev);
	}
}

/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  EEPROM 保存图片数据
 *
 * @Param data  图片地址
 */
/* ----------------------------------------------------------------------------*/
void eeprom_save_data(rt_uint8_t *data)
{
	rt_device_t dev;

	dev = eeprom_device_open();
	rt_device_write(dev, EEPROM_PIC0_ADDR, data, ELS_PIC_BUF_SIZE);
	eeprom_device_close(dev);
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  EEPROM 加载图片数据
 *
 * @Param data 图片数据缓存
 */
/* ----------------------------------------------------------------------------*/
void eeprom_load_data(rt_uint8_t *data)
{
	rt_device_t dev;

	dev = eeprom_device_open();
	rt_device_read(dev, EEPROM_PIC0_ADDR, data, ELS_PIC_BUF_SIZE);
	eeprom_device_close(dev);
}



void get_dev_mac(rt_uint8_t *id)
{
	rt_uint32_t CpuID[3];
	
	//获取CPU唯一ID
	CpuID[0]=*(int *)(0x1ffff7e8);
	CpuID[1]=*(int*)(0x1ffff7ec);
	CpuID[2]=*(int*)(0x1ffff7f0);
	//转换为字节
	uint32_to_str(CpuID[0], id);
	uint32_to_str(CpuID[1], &id[4]);
	uint32_to_str(CpuID[2], &id[8]);
}

rt_uint8_t devinfo_regstate_get(void)
{
	return devInfoDat.regFlag;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  加载EEPROM到设备信息结构体
 *
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
rt_err_t devinfo_loader(void)
{
	rt_device_t dev;
	rt_size_t size;
	
	dev = eeprom_device_open();
	size = rt_device_read(dev, EEPROM_DEVINFO_ADDR, &devInfoDat, sizeof(devInfodef));
	eeprom_device_close(dev);
	if(size != sizeof(devInfodef))
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  保存设备信息到EEPROM
 *
 * @Returns   
 */
/* ----------------------------------------------------------------------------*/
rt_err_t devinfo_save(void)
{
	rt_device_t dev;
	rt_size_t size;

	dev = eeprom_device_open();
	size = rt_device_write(dev, EEPROM_DEVINFO_ADDR, &devInfoDat, sizeof(devInfodef));
	eeprom_device_close(dev);
	if(size != sizeof(devInfodef))
	{
		return RT_ERROR;	
	}
	return RT_EOK;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  获取设备信息结构体
 *
 * @Param data
 */
/* ----------------------------------------------------------------------------*/
devInfodef* devinfo_get(void)
{
	rt_kprintf("***************************************%s\n",devInfoDat.url);
	return &devInfoDat;
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  设置设备信息结构体
 *
 * @Param data
 */
/* ----------------------------------------------------------------------------*/
void devinfo_set(devInfodef *data)
{
	devInfoDat = (*data);
}


void save_sysflow_bkp(void)
{
	devInfodef *info;
	rt_uint16_t data[2];

	info = devinfo_get();
	data[0] = info->flow & 0x0000ffff;
	data[1] = ((info->flow>>16) & 0x0000ffff);
	BKP_WriteBackupRegister(BKP_DR3,data[0]);
	BKP_WriteBackupRegister(BKP_DR4,data[1]);
}

void set_bkp1_reg(rt_uint16_t value)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	PWR_BackupAccessCmd(ENABLE);
	BKP_WriteBackupRegister(BKP_DR1,value);
}

void load_bkp_sys_flow(void)
{
	devInfodef *info;
	rt_uint16_t data[2];

	info = devinfo_get();
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	data[0] = BKP_ReadBackupRegister(BKP_DR3);
	data[1] = BKP_ReadBackupRegister(BKP_DR4);

	info->flow = data[1];
	info->flow <<= 16;
	info->flow += data[0];
}

void save_bkp_wakeup_num(rt_uint16_t value)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	PWR_BackupAccessCmd(ENABLE);
	BKP_WriteBackupRegister(BKP_DR5,value);

}

rt_uint16_t load_bkp_wakeup_num(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	PWR_BackupAccessCmd(ENABLE);
	return BKP_ReadBackupRegister(BKP_DR5);
}

void save_bkp_askfail_num(rt_uint16_t value)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	PWR_BackupAccessCmd(ENABLE);
	BKP_WriteBackupRegister(BKP_DR6,value);

}

rt_uint16_t load_bkp_askfail_num(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);// PWR的使能。
	PWR_BackupAccessCmd(ENABLE);
	return BKP_ReadBackupRegister(BKP_DR6);
}


rt_uint8_t get_sys_vbat(rt_uint32_t *value)
{
	rt_device_t dev;
	rt_err_t result;
	rt_uint8_t vbat;

	vbat = 0;
	dev = rt_device_find(DEVICE_NAME_VBAT);
	if(dev != RT_NULL)
	{
		result = rt_device_open(dev, RT_DEVICE_FLAG_RDONLY);
		if(result == RT_EOK)
		{
			rt_uint32_t value;

			if(rt_device_read(dev, 0, &value, 4) == 0);
			{
				rt_uint32_t Voltage;
				
				Voltage = (value*750)>>10;
				rt_kprintf("get adc value %d Voltage=%d mV\n",value,Voltage);
				if(value < VBAT_LOW_ENERGE)//电池不够电量
				{
					rt_uint16_t bkp=0;
					
					vbat = 0;
					bkp = read_rtc_bkp1();
					bkp |= SYS_EVT_VBATLOW;
					set_bkp1_reg(bkp);//标记没电
				}
				else if(value > VBAT_FULL_ENERGE)
				{
					rt_uint16_t bkp=0;
					
					vbat = 100;
					bkp = read_rtc_bkp1();
					bkp |= SYS_EVT_VBATFULL;
					set_bkp1_reg(bkp);//标记充满电

					if((bkp & SYS_EVT_VBATFULL) && (bkp & SYS_EVT_VBATLOW))
					{
						devInfodef *info;

						info = devinfo_get();
						info->charge += 1;
						bkp &= (~SYS_EVT_VBATLOW);
						bkp &= (~SYS_EVT_VBATLOW);
						set_bkp1_reg(bkp);
						devinfo_save();//保存充电次数
					}
				}
				else
				{
					vbat = (value-VBAT_LOW_ENERGE)*100/(VBAT_FULL_ENERGE-VBAT_LOW_ENERGE);
					rt_kprintf("vbat=%d\n",vbat);
				}
			}
			rt_device_close(dev);
		}
		else
		{
			rt_kprintf("Open %s device fail\n",DEVICE_NAME_VBAT);
		}
	}
	else
	{
		rt_kprintf("None find %s device\n",DEVICE_NAME_VBAT);
	}

	return vbat;
}


void vbat_init_process(void)
{
	devInfodef *info;

	info = devinfo_get();
	
	info->vbat = get_sys_vbat(RT_NULL);//采集电压
	rt_kprintf("系统电池%d\n",info->vbat);
	if(info->vbat < 5)//电压过低
	{
		rt_kprintf("系统电压过低%d %\n",info->vbat);
		send_epd_mail(EPD_EVT_BATAlA);//显示电池欠压标记
	}
}


void date_show(time_t now)
{
	//time_t now;

	//now = time(RT_NULL);
	rt_kprintf("%s\n", ctime(&now));
}

rt_uint32_t cur_date_show(void)
{
	rt_device_t dev;
	rt_time_t date;

	dev = rt_device_find("rtc");
	if(dev != RT_NULL)
	{
		rt_device_init(dev);
		rt_device_open(dev,RT_DEVICE_FLAG_RDWR);
		rt_device_control(dev, RT_DEVICE_CTRL_RTC_GET_TIME, &date);
		rt_device_close(dev);
	}
	rt_kprintf("cur date %d\n",date);
	rt_kprintf("%s\n", ctime((const time_t*)&date));
	return date;
}

void update_rtc_value(rt_uint32_t date)
{
	rt_device_t dev;

	dev = rt_device_find("rtc");
	if(dev != RT_NULL)
	{
		rt_device_init(dev);
		rt_device_open(dev,RT_DEVICE_FLAG_RDWR);
		rt_device_control(dev, RT_DEVICE_CTRL_RTC_SET_TIME, &date);
		rt_device_close(dev);
	}
	else
	{
		rt_kprintf("None Find RTC Device\n");
	}
}


void cycle_update_show(void)
{
	rt_uint16_t wakupNum;
	rt_uint32_t time;
	rt_uint16_t *cycle;

	cycle = update_ask_cycle_get();

	wakupNum = load_bkp_wakeup_num();
	rt_kprintf("系统重启%d\n",wakupNum);
	time = wakupNum * (*cycle);
	time /= 3600;
	if(time == 24)//每天更新一次价签。
	{
		rt_uint16_t bkp=0;

		rt_kprintf("刷新屏幕\n");
		wakupNum = 0;
		save_bkp_wakeup_num(wakupNum);
					
		bkp = read_rtc_bkp1();
		bkp &= (~SYS_EVT_WAKEUP);//清掉唤醒
		set_bkp1_reg(bkp);//标记没电
	}
	else
	{
		wakupNum++;
		rt_kprintf("wakupNum = %d\n",wakupNum);
		save_bkp_wakeup_num(wakupNum);
	}
}

